<a href="http://github.com/angular/angular.js/edit/master/docs/content/guide/dev_guide.services.understanding_services.ngdoc" class="improve-docs btn btn-primary"><i class="icon-edit"> </i> Improve this doc</a><h1><code ng:non-bindable=""></code>
<div><span class="hint"></span>
</div>
</h1>
<div><div class="developer-guide-page developer-guide-angular-services-understanding-angular-services-page"><h3 id="what-are-angular-services">What are Angular Services?</h3>
<p>Angular services are singletons objects or functions that carry out specific tasks common to web apps.
Angular has a number of built in services, such as the <a href="api/ng.$http"><code>$http service</code></a>, which
provides access to the browser&#39;s <code>XMLHttpRequest</code> object for making requests to a server. Like other core
Angular variables and identifiers, the built-in services always start with <code>$</code> (such as <code>$http</code> mentioned
above). You can also create your own custom services.</p>
<h3 id="using-a-service">Using a Service</h3>
<p>To use an Angular service, you identify it as a dependency for the component (controller, service,
filter or directive) that depends on the service.  Angular&#39;s dependency injection subsystem takes
care of the rest. The Angular injector subsystem is in charge of service instantiation, resolution
of dependencies, and provision of dependencies to components as requested.</p>
<p>Angular injects dependencies using
<a href="http://misko.hevery.com/2009/02/19/constructor-injection-vs-setter-injection/">&quot;constructor&quot; injection</a>.
The dependency is passed to the component&#39;s factory/constructor function. Because JavaScript is a dynamically
typed language, Angular&#39;s dependency injection subsystem cannot use static types to identify service
dependencies. For this reason a component must, explicitly, define its dependencies by using one of the
<a href="guide/di">injection annotation</a> methods.  For example, by providing a <code>$inject</code> property:</p>
<pre><code>    var MyController = function($location) { ... };
    MyController.$inject = [&#39;$location&#39;];
    myModule.controller(&#39;MyController&#39;, MyController);</code></pre>
<p>Or by providing an &quot;inline&quot; injection annotation:</p>
<pre><code>    var myService = function($http) { ... };
    myModule.factory(&#39;myService&#39;, [&#39;$http&#39;, myService]);</code></pre>
<h3 id="defining-a-service">Defining a Service</h3>
<p>Application developers are free to define their own services by registering their name, and <strong>service
factory function</strong>, in Angular modules.</p>
<p>The purpose of the <strong>service factory function</strong> is to generate the single object, or function, that 
represents the service to the rest of the application. That object, or function, will then be
injected into any component (controller, service, filter or directive) that specifies a dependency
on the service.</p>
<p>Angular factory functions are executed lazily. That is, they are only executed when needed 
to satisfy a dependency, and are then executed exactly once for each service. Everything that is
dependent on this service gets a reference to the single instance generated by the service factory.</p>
<h3 id="related-topics">Related Topics</h3>
<ul>
<li><a href="guide/di">About Angular Dependency Injection</a></li>
<li><a href="guide/dev_guide.services.creating_services">Creating Angular Services</a></li>
<li><a href="guide/dev_guide.services.managing_dependencies">Managing Service Dependencies</a></li>
<li><a href="guide/dev_guide.services.testing_services">Testing Angular Services</a></li>
</ul>
<h3 id="related-api">Related API</h3>
<ul>
<li><a href="api/ng">Angular Service API</a></li>
<li><a href="api/angular.injector"><code>Injector API</code></a></li>
</ul>
</div></div>
